package com.um2.mauction.java;

import java.io.IOException;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import com.um2.mauction.session.impl.CategorieManagerBean;
import com.um2.mauction.session.remote.CategorieManagerRemote;

public class ListCategorieAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		
		ActionForward resultat = mapping.findForward("succes");
		ActionErrors errors = new ActionErrors();
		
		CategorieManagerRemote cmanager=null;
		
		try
		{	
			InitialContext ic = new InitialContext();
			cmanager = (CategorieManagerRemote)ic.lookup(CategorieManagerBean.RemoteJNDIName);
		
			List listeCategories = cmanager.getAllCategories();
			
			if (listeCategories==null)
			{	
				errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("list.listecategories.null"));
				resultat = new ActionForward(mapping.getInput());					
			}
			else
			{
				//if (mapping.getScope().equals("request"))
				//{
					request.setAttribute("listeCategories",listeCategories);
				//}
				//else 
				//{
				//	System.out.println("type non request");
				//}
				//response.setParameter("listeCategories",listeCategories);
			}
			
		}
		catch(NamingException ex)
		{
			System.out.println("ERREUR!!" + ex.getMessage());
		}		
		
		if (!errors.isEmpty())
		{
			saveErrors(request, errors);
		}
		return resultat;
	}	
}
